package net.wofly.right.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.domain.Organization;
import net.wofly.right.domain.User;
import net.wofly.right.service.IOrganizationService;
import net.wofly.right.validator.OrganizationValidator;

@Controller
@RequestMapping(value = "/api/right/org")
@Api(description = "组织机构")
public class OrganizationController {
    
    private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";
    Logger logger = LoggerFactory.getLogger(OrganizationController.class);
    @Autowired
    IOrganizationService iOrganizationService;
    
    @ApiOperation(value = "根据组织机构编码查询子机构")
    @ApiImplicitParams({@ApiImplicitParam(name = "orgCode", value = "组织机构编码", required = true, dataType = "String",
            paramType = "form"), @ApiImplicitParam(name = "status", value = "状态（1：有效；0，无效； 默认查询有效状态）",
            required = true, dataType = "int", paramType = "form"), @ApiImplicitParam(name = "pageNum", value =
            "页码（默认第一页）", required =
            true, dataType = "int", paramType = "form"), @ApiImplicitParam(name = "pageSize", value =
            "每页显示条数（默认10条）", required = true, dataType = "int", paramType = "form")})
    @RequestMapping(value = "/findSubOrgsByOrgCode", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<Organization>> findSubOrgsByOrgCode(
    		@RequestParam(defaultValue = "100") String orgCode, 
    		@RequestParam(defaultValue = "1") Integer status,
        @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
        @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        String errorMessage = OrganizationValidator.validateOrgCode(orgCode);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iOrganizationService.findSubOrgsByOrgCode(orgCode, status, pageNum, pageSize);
    }
    
    
    @ApiOperation(value = "修改组织机构")
    @ApiImplicitParams({@ApiImplicitParam(name = "organization", value = "组织机构实体", required = true, dataType =
            "Organization")})
    @RequestMapping(value = "/modifyOrganization", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Organization> modifyOrganization(@RequestBody Organization organization) {
        String errorMessage = OrganizationValidator.validateModifyOrganizationParams(organization);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iOrganizationService.modifyOrganization(organization);
    }
    
    @ApiOperation(value = "根据组织机构编码查询员工")
    @ApiImplicitParams({@ApiImplicitParam(name = "orgCode", value = "组织机构编码", required = true, dataType = "String",
            paramType = "form"), @ApiImplicitParam(name = "status", value = "状态（1：有效；0，无效； 默认查询有效状态）",
            required = true, dataType = "int", paramType = "form"), @ApiImplicitParam(name = "pageNum", value =
            "页码（默认第一页）", required = true, dataType = "int", paramType = "form"), @ApiImplicitParam(name = "pageSize",
            value = "每页显示条数（默认10条）", required = true, dataType = "int", paramType = "form")})
    @RequestMapping(value = "/findSubUsersByOrgCode", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<PageWrapper<User>> findSubUsersByOrgCode(
            String orgCode, @RequestParam(defaultValue = "1") Integer status,
            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        String errorMessage = OrganizationValidator.validateOrgCode(orgCode);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iOrganizationService.findSubUsersByOrgCode(orgCode, status, pageNum, pageSize);
    }
    
    @ApiOperation(value = "添加子机构")
    @ApiImplicitParams({@ApiImplicitParam(name = "organization", value = "组织机构实体", required = true, dataType =
            "Organization")})
    @RequestMapping(value = "/addSubOrganization", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Organization> addSubOrganization(String parentOrgID, @RequestBody Organization organization) {
        String errorMessage = OrganizationValidator.validateAddSubOrganizationParams(parentOrgID,organization);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iOrganizationService.addSubOrganization(parentOrgID,organization);
    }
    
    @ApiOperation(value = "删除组织机构")
    @ApiImplicitParams({@ApiImplicitParam(name = "organizationID", value = "组织机构实体", required = true, dataType =
            "string",paramType = "form")})
    @RequestMapping(value = "/deleteOrganization", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Void> deleteOrganization(String organizationID) {
        String errorMessage = OrganizationValidator.validateOrganizationID(organizationID);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iOrganizationService.deleteOrganization(organizationID);
    }
    
    @ApiOperation(value = "根据组织机构编码查询子机构")
    @ApiImplicitParams({@ApiImplicitParam(name = "orgCode", value = "组织机构编码", required = true, dataType = "String",paramType = "form"), 
    						@ApiImplicitParam(name = "status", value = "状态（1：有效；0，无效； 默认查询有效状态）",required = true, dataType = "int", paramType = "form")})
    @RequestMapping(value = "/findOrgTreeByOrgCode", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<Organization>> findOrgTreeByOrgCode(
										    		@RequestParam(defaultValue = "100") String orgCode, 
										    		@RequestParam(defaultValue = "1") Integer status) {
        String errorMessage = OrganizationValidator.validateOrgCode(orgCode);
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }
        return iOrganizationService.findOrgTreeByOrgCode(orgCode ,status );
    }
}
